package Aleehot100;

import java.util.Arrays;

/**
 * ClassName: c012
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/24 13:11
 * @Version 1.0
 * @Since 1.0
 */
public class c012 {
    public static void main(String[] args) {
        String str = "ADOBECODEBANC";
        String t = "ABC";
        System.out.println(minWindow(str, t));
    }
    static int[] count = new int[256];
    // 字符串
    public static String minWindow(String s, String t) {
        Arrays.fill(count, 0);
        char[] sArr = s.toCharArray();
        char[] tArr = t.toCharArray();

        // 统计次数
        int diff = tArr.length;
        for (int i = 0; i < tArr.length; i++) {
            count[tArr[i]] --;
        }

        // 统计答案
        int len = Integer.MAX_VALUE, start = Integer.MAX_VALUE;
        for (int l = 0, r = 0; r < sArr.length; r++) {
            if(count[sArr[r]] ++ < 0) {
                diff --;
            }
            if(diff == 0) {
                // 是否能压缩
                while(count[sArr[l]] > 0) {
                    count[sArr[l ++]] --;
                }

                if(r - l + 1 < len) {
                    start = l;
                    len = r - l + 1;
                }
            }
        }
        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }
}
